from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import ctime, sleep

driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
driver.maximize_window()

# 显式等待1
element = WebDriverWait(driver, 5, 0.5, ).until(
    EC.visibility_of_all_elements_located((By.ID, 'kw'))
)
element.send_keys('selenium')



# 显式等待2
"""
用displayed判断元素是否可见
"""
print(ctime())
for i in range(10):
    try:
        kw = driver.find_element_by_id('kw1')
        if kw.is_displayed():
            break
    except:
        pass
    sleep(1)
else:
    print('time out')
print(ctime())

driver.quit()
